//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { Image as VanImage } from "vant";
import { scrollMixin } from "utils/mixin";
export default {
  name: "",
  //import引入的组件需要注入到对象中才能使用
  components: {
    VanImage
  },
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      userInfo: {},
      tabList: [
        {
          name: "全部",
          status: ""
        },
        {
          name: "未结算",
          status: 0
        },
        {
          name: "已结算",
          status: 1
        }
      ],
      name: "",
      status: "",
      page: 1,
      isLoadMore: true,
      total_page: 0,
      recordList: []
    };
  },
  //监听属性 类似于data概念
  computed: {
    rechargeImg() {
      if (this.userInfo.member && this.userInfo.member && this.userInfo.member.avatar_image) {
        return this.userInfo.member.avatar_image
      }
      return ''
    },
    recharge_settled() {
      return this.userInfo.settled || '0.00'
    },
    recharge_unsettled() {
      return this.userInfo.unsettled || '0.00'
    },
    check_level_name(){
      if(this.userInfo && this.userInfo.team_dividend && this.userInfo.team_dividend.has_one_level && this.userInfo.team_dividend.has_one_level.level_name){
        return true
      }else{
        return false
      }
    },
    r_level_name(){
      if(this.userInfo && this.userInfo.team_dividend && this.userInfo.team_dividend.has_one_level && this.userInfo.team_dividend.has_one_level.level_name){
        return this.userInfo.team_dividend.has_one_level.level_name
      }
    }
  },
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    ontabClick(event) {
      this.status = event;
      this.page = 1;
      console.log(this.status)
      this.getRecordList();
    },
    recordUser() {
      //会员信息
      $http
        .post("plugin.region-external-reward.frontend.controllers.record.bonus-record.get-top-data", {}, "loading")
        .then(res => {
          if (res.result === 1) {
            let data = res.data;
            this.userInfo = data;
            if(this.userInfo.income_name) {
              this.fun.setWXTitle(this.userInfo.income_name);
            }
          } else {
            this.$dialog.alert({
              message: res.msg
            });
          }
        })
        .catch(err => {
          console.log("getUserData::err", err);
        });
    },
    getRecordList() {
      let query = {};
      let url = "plugin.region-external-reward.frontend.controllers.record.bonus-record.index";
      if (this.$route.name == "phone_bill_recharge_dealer") {
        url = "plugin.phone-bill-recharge.frontend.settlement.index";
        query = {
          page: this.page,
          status: this.status === 0 ? 'settled' : this.status == 1 ? 'unsettled' : ''
        }
      } else {
        query = {
          page: this.page,
          status: this.status
        }
      }
      $http.get(url, query, "加载中..").then(response => {
        if (response.result == 1) {
          let res = response.data;
          if (this.$route.name == "phone_bill_recharge_dealer") {
            this.userInfo = res;
            this.recordList = res.settlements.data;
            this.isLoadMore = true;
            this.total_page = res.settlements.last_page;
            if (!this.total_page) {
              this.page = 0;
            }
            return
          } else {
            let recordList = res.bonus_record_list.data;
            this.isLoadMore = true;
            this.total_page = res.bonus_record_list.last_page;
            if (!this.total_page) {
              this.page = 0;
            }
            this.recordList = recordList;
          }
        } else {
          this.page = this.page - 1;
          this.isLoadMore = false;
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let query = {};
        let url = "plugin.region-external-reward.frontend.controllers.record.bonus-record.index";
        if (this.$route.name == "phone_bill_recharge_dealer") {
          url = "plugin.phone-bill-recharge.frontend.settlement.index";
          query = {
            page: this.page,
            status: this.status === 0 ? 'settled' : this.status == 1 ? 'unsettled' : ''
          }
        } else {
          query = {
            page: this.page,
            status: this.status
          }
        }
        $http.post(url, query, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let data = res.data;
              if(this.$route.name == "phone_bill_recharge_dealer"){
                this.recordList = this.recordList.concat(res.settlements.data); //数组拼接
                return
              }
              let recordList = data.bonus_record_list.data;
              this.recordList = [...this.recordList, ...recordList]; //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          function (response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    }
  },
  activated() {
    if (this.$route.name != "phone_bill_recharge_dealer") {
      this.recordUser();
    }
    if (this.$route.name == "phone_bill_recharge_dealer") {
      this.name = "phone_bill_recharge_dealer"
    }
    this.getRecordList();
  },
};
